package com.ringtheweb.web.bpm;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.calendar.BusinessCalendar;
import org.jbpm.calendar.Duration;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.job.Timer;
import org.jbpm.scheduler.SchedulerService;
import org.jbpm.svc.Services;

public class ChangeDueDateActionHandler implements ActionHandler {
    private static final long serialVersionUID = 1705L;
    private static final Log log = LogFactory.getLog(ChangeDueDateActionHandler.class);

    static BusinessCalendar businessCalendar = new BusinessCalendar();

    String timerName;
    String delay;

    public void execute(ExecutionContext executionContext) throws Exception {
         try {
              Timer timer = executionContext.getTimer();
              if (timer != null && timerName.equals(timer.getName())) {
                   String dueDate = delay;
                               // get the value of a variable
                   
                   if (delay.startsWith("#{")) {
                        dueDate = (String) executionContext.getVariable(delay.substring(2, delay.length() -1));
                   }
                               log.info("Changing the timer: " + timer + " to fire in " + dueDate);

                   SchedulerService schedulerService = (SchedulerService) Services.getCurrentService(Services.SERVICENAME_SCHEDULER);

                   // delete the existing timer
                   schedulerService.deleteTimersByName(timer.getName(), executionContext.getToken());

                   // create a new one with the right delay
                   Duration duration = new Duration(dueDate);
                   Date dueDateDate = businessCalendar.add(new Date(), duration);
                   timer.setDueDate(dueDateDate);
                   schedulerService.createTimer(timer);
              } else {
                   log.debug("Doesn't match: " + timer);
              }
         } catch (Exception ex) {
              ex.printStackTrace();
         }
    }

    public String getDelay() {
         return delay;
    }

    public void setDelay(String delay) {
         this.delay = delay;
    }

    public String getTimerName() {
         return timerName;
    }

    public void setTimerName(String timerName) {
         this.timerName = timerName;
    }
}

